package cn.itcast.xxl.job;
import org.springframework.data.redis.core.StringRedisTemplate;
import com.itheima.health.utils.aliyunoss.AliyunUtils;
import com.itheima.health.utils.resources.RedisConstant;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.Set;

@JobHandler(value = "heima.setmeal.clean.img.job") //  web配置JobHandler的名称
@Component
public class MyJob extends IJobHandler {

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 但是spring的@Scheduled只支持6位，年份是不支持的，带年份的7位格式会报错：
     * Cron expression must consist of 6 fields (found 7 in "1/5 * * * * ? 2018")
     * 通过cron表达式 来配置 该方法的执行周期
     **/

    @Override
    public ReturnT<String> execute(String s) throws Exception {

        Set<String> sdiff = redisTemplate.opsForSet().difference(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);

        System.out.println("差值长度"+":"+sdiff.size());

        Set setmealPicResources = redisTemplate.opsForSet().members(RedisConstant.SETMEAL_PIC_RESOURCES);
        Set setmealPicDbResources = redisTemplate.opsForSet().members( RedisConstant.SETMEAL_PIC_DB_RESOURCES);

        System.out.println("仓库1:"+setmealPicResources+"--"+"仓库2:"+setmealPicDbResources);


        if (sdiff.size()>0) {
            for (String filename : sdiff) {
                String s1 = filename.replaceAll("https://heimazzy.oss-cn-shanghai.aliyuncs.com/", "");
                AliyunUtils.deleteFile(s1); //  删除阿里云上的多余图片
            }
            redisTemplate.delete(RedisConstant.SETMEAL_PIC_RESOURCES);
            redisTemplate.delete(RedisConstant.SETMEAL_PIC_DB_RESOURCES);
            System.out.println("-setmeal--is-scanning---one-more");
        } else {
            System.out.println("-xxl-job--is-running----");

        }
        return SUCCESS;
    }
}
